
import sys, os, re
import urllib
from dateutil import parser as dateparser
from baseHandler import *

__author__="aalsum"
__date__ ="$Mar 5, 2013 2:24:07 PM$"

baseuri = "http://wayback.webarchiv.cz/wayback/query?type=urlquery&url="

class CzechHandler(BaseProxyHandler):

    def fetch_changes(self, req, requri, dt=None):

        uri = baseuri + requri
        try:
            fh = urllib.urlopen(uri)
        except:
            return self.error("Couldn't retrieve data from %s" % uri, req, status=404)
        data = fh.read()
        fh.close()

        regex = r'href="http://wayback.webarchiv.cz/wayback/[^"]+';
        uriRegex = re.compile( regex )

        changes = []
        uris = re.findall(uriRegex, data)
        for u in uris:
            dtstr = u[42:56]
            loc = u[6:-1]
            dtstr += " GMT"
            dtobj = dateparser.parse(dtstr)
            changes.append((dtobj, loc, {'last': dtobj, 'obs' : 1}))

        return changes



def handler(req):
    hdlr = CzechHandler('cz')
    return basehandler(req, hdlr)


